﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PublicModelsAndEntities.Models.UserAndRole
{
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// 通用请求实体.
    /// </summary>
    public class RequestPayload
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RequestPayload"/> class.
        /// </summary>
        public RequestPayload()
        {
            this.Sort = new List<SortModel>();
            this.Kw = string.Empty;
        }

        /// <summary>
        /// Gets or sets 分页大小.
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// Gets or sets 当前页码.
        /// </summary>
        public int CurrentPage { get; set; }

        /// <summary>
        /// Gets or sets 排序对象集合(支持多个字段排序).
        /// </summary>
        public List<SortModel> Sort { get; set; }

        /// <summary>
        /// Gets 组合后的排序字符串.
        /// </summary>
        public string OrderBy
        {
            get
            {
                var orderBy = string.Empty;
                var sort = this.Sort.Where(x => string.IsNullOrEmpty(x.Field) && string.IsNullOrEmpty(x.Direct)).ToList();
                if (sort.Count > 0)
                {
                    orderBy = "ORDER BY " + string.Join(",", sort.Select(x => $"{x.Field} {x.Direct}"));
                }

                return orderBy;
            }
        }


        /// <summary>
        /// Gets 第一个排序字段(单个字段排序).
        /// </summary>
        public SortModel FirstSort
        {
            get
            {
                var fs = this.Sort[0];
                if (fs == null)
                {
                    return null;
                }

                var sort = new SortModel
                {
                    Direct = fs.Direct.ToUpper(),
                    Field = fs.Field,
                };
                return sort;
            }
        }

        /// <summary>
        /// Gets or sets 搜索关键字.
        /// </summary>
        public string Kw { get; set; }

        /// <summary>
        /// 排序实体对象.
        /// </summary>
        public class SortModel
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="SortModel"/> class.
            /// 排序实体对象构造函数.
            /// </summary>
            public SortModel()
            {
                this.Direct = "DESC";
            }

            /// <summary>
            /// Gets or sets 排序字段.
            /// </summary>
            public string Field { get; set; }

            /// <summary>
            /// Gets or sets 排序方向.
            /// </summary>
            public string Direct { get; set; }
        }
    }
}
